package cc.openkit.admin.controller.adv.type;

import cc.openkit.admin.model.AdvHello;
import cc.openkit.admin.model.AdvType;
import cc.openkit.admin.model.GGroupLimit;
import cc.openkit.admin.service.adv.type.AdvTypeService;
import cc.openkit.admin.service.g.GGroupLimitService;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.common.KitUtil;
import cc.openkit.kitIsNull.KitVerification;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告类型管理
 *
 * @author bigben
 * @date 2018.4.2
 */
@Controller
@Scope("prototype")
@RequestMapping("/adv_type")
public class AdvTypeController {
    private Logger log = Logger.getLogger(AdvTypeController.class);

    @Resource
    private AdvTypeService advTypeService;
    @Resource
    private GGroupLimitService gGroupLimitService;

    /**
     * 去首次打开APP广告列表页面
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView index(HttpServletRequest request){
        log.info("去首次打开APP广告类型列表页面");

        // 权限验证
        GGroupLimit gGroupLimit = gGroupLimitService.testGroup(request, 31);
        ModelAndView mv = new ModelAndView();
        // 没有权限，返回错误页面
        if(gGroupLimit==null){
            mv.setViewName("/admin/err/no_group_err");
            mv.addObject("msg", StaticFinalVar.NO_GROUP_MSG+StaticFinalVar.CALL_GROUP);
            return mv;
        }
        // 取值
        mv.addObject("kitG",gGroupLimit);
        mv.setViewName("/admin/adv_type/list");
        return mv;
    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "/getAllJson", method = RequestMethod.POST)
    @ResponseBody
    public Object getAllJson(HttpServletRequest request){
        log.info("获取APP广告类型列表的数据");

        Map<String,Object> map = new HashMap<String,Object>();

        String page = request.getParameter("page");// 获得页数
        String limit = request.getParameter("limit");// 获得每页显示条数
        String search = request.getParameter("search");// 获取搜索条件

        // 封装数据
        AdvType advType = new AdvType();
        if(KitUtil.feikong(search)){
            advType.setName(search);
        }

        // 分页查询
        List<AdvType> list = advTypeService.queryPageListByWhere(advType, Integer.valueOf(page), Integer.valueOf(limit));
        int size = advTypeService.queryCount(advType);

        // 返回数据
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",list);

        return JSONObject.toJSON(map);

    }

    /**
     * 去添加页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/goAdd", method = RequestMethod.GET)
    public Object goAdd(HttpServletRequest request){
        log.info("广告模型类型 》 添加 》 跳转");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/adv_type/add");
        return mv;
    }

    /**
     * 添加
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(HttpServletRequest request) throws Exception{
        log.info("广告模型添加 》 添加 》 保存");

        // 取值
        String name = request.getParameter("name");
        String action = request.getParameter("action");
        String titleType = request.getParameter("titleType");
        String explainType = request.getParameter("explainType");
        String priceType = request.getParameter("priceType");
        String timeType = request.getParameter("timeType");
        String subheadType = request.getParameter("subheadType");
        String imgType = request.getParameter("imgType");
        String editorType = request.getParameter("editorType");

        // 封装对象
        AdvType advType = new AdvType();
        advType.setId(KitUtil.uuid());
        advType.setName(name);
        advType.setAction(action);
        Map<String, Object> map = KitVerification.verification(advType);
        if(!"200".equals(map.get("code"))){
            return JSONObject.toJSON(map);
        }
        advType.setTitleType("on".equals(titleType)?"1":"2");
        advType.setExplainType("on".equals(explainType)?"1":"2");
        advType.setPriceType("on".equals(priceType)?"1":"2");
        advType.setTimeType("on".equals(timeType)?"1":"2");
        advType.setSubheadType("on".equals(subheadType)?"1":"2");
        advType.setImgType("on".equals(imgType)?"1":"2");
        advType.setEditorType("on".equals(editorType)?"1":"2");

        // 保存并返回
        return JSONObject.toJSON(advTypeService.save(advType)==1 ? KitUtil.returnMap("200",StaticFinalVar.ADD_OK) : KitUtil.returnMap("101",StaticFinalVar.ADD_ERR));

    }

    /**
     * 修改开关状态
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateType", method = RequestMethod.POST)
    @ResponseBody
    public Object updateType(HttpServletRequest request) throws Exception{
        log.info("修改对应的开关状态");
        //取值
        String id = request.getParameter("id");
        String kaiguan = request.getParameter("kaiguan");
        String name = request.getParameter("name");

        // 封装
        AdvType advType = new AdvType();
        advType.setId(id);
        if(!KitUtil.feikong(name)){
            return KitUtil.returnMap("101","缺少参数");
        }

        if("titleType".equals(name)){
            advType.setTitleType(kaiguan);
        }
        if("explainType".equals(name)){
            advType.setExplainType(kaiguan);
        }
        if("priceType".equals(name)){
            advType.setPriceType(kaiguan);
        }
        if("timeType".equals(name)){
            advType.setTimeType(kaiguan);
        }
        if("subheadType".equals(name)){
            advType.setSubheadType(kaiguan);
        }
        if("imgType".equals(name)){
            advType.setImgType(kaiguan);
        }
        if("editorType".equals(name)){
            advType.setEditorType(kaiguan);
        }

        return advTypeService.updateSelective(advType) == 1 ? KitUtil.returnMap("200",StaticFinalVar.UPDATE_OK) : KitUtil.returnMap("101",StaticFinalVar.UPDATE_ERR);
    }

    /**
     * 删除
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Object del(HttpServletRequest request) throws Exception{
        log.info("删除广告状态");
        String id = request.getParameter("id");
        int i = advTypeService.deleteByUUId(id);
        return i==1?KitUtil.returnMap("200",StaticFinalVar.DEL_OK):KitUtil.returnMap("101",StaticFinalVar.DEL_ERR);
    }

    /**
     * 修改前的查询
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/goUpdate", method = RequestMethod.GET)
    public ModelAndView goUpdate(HttpServletRequest request) throws Exception{
        String id = request.getParameter("id");
        AdvType model = advTypeService.queryByUUID(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/adv_type/update");
        mv.addObject("kitModel",model);
        return mv;
    }

    /**
     * 修改
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request) throws Exception{
        // 取值
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String action = request.getParameter("action");
        // 封装对象
        AdvType advType = new AdvType();
        advType.setId(KitUtil.uuid());
        advType.setName(name);
        advType.setAction(action);
        Map<String, Object> map = KitVerification.verification(advType);
        if(!"200".equals(map.get("code"))){
            return JSONObject.toJSON(map);
        }

        // 先去根据id查询这个条数
        AdvType at = advTypeService.queryByUUID(id);
        if(!name.equals(at.getName())){
            AdvType a = new AdvType();
            a.setName(name);
            Integer integer = advTypeService.queryCount(a);
            if(integer>0){
                return JSONObject.toJSON(KitUtil.returnMap("101",StaticFinalVar.IS_NOT_NULL));
            }
        }

        String titleType = request.getParameter("titleType");
        String explainType = request.getParameter("explainType");
        String priceType = request.getParameter("priceType");
        String timeType = request.getParameter("timeType");
        String subheadType = request.getParameter("subheadType");
        String imgType = request.getParameter("imgType");
        String editorType = request.getParameter("editorType");


        advType.setId(id);
        advType.setTitleType("on".equals(titleType)?"1":"2");
        advType.setExplainType("on".equals(explainType)?"1":"2");
        advType.setPriceType("on".equals(priceType)?"1":"2");
        advType.setTimeType("on".equals(timeType)?"1":"2");
        advType.setSubheadType("on".equals(subheadType)?"1":"2");
        advType.setImgType("on".equals(imgType)?"1":"2");
        advType.setEditorType("on".equals(editorType)?"1":"2");

        // 修改保存并返回参数
        return JSONObject.toJSON(advTypeService.updateSelective(advType)==1 ? KitUtil.returnMap("200",StaticFinalVar.UPDATE_OK) : KitUtil.returnMap("101",StaticFinalVar.UPDATE_ERR));
    }

    /**
     * 获取对象接口
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getModel", method = RequestMethod.POST)
    @ResponseBody
    public Object getModel(HttpServletRequest request) throws Exception{
        log.info("删除广告状态");
        String id = request.getParameter("id");
        System.out.printf("id="+id);
        AdvType advType = advTypeService.queryByUUID(id);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("KitModel",advType);
        return JSONObject.toJSON(map);
    }

}
